1 using UnityEngine;
2 using
System.Collections;
3
4 [RequireComponent (
typeof (Detonator))]
5 [AddComponentMenu(
"Detonator/Sound")]
6 public
class DetonatorSound : DetonatorComponent {
7     
8     
public AudioClip[] nearSounds;
9     
public AudioClip[] farSounds;
10     
11     
public float distanceThreshold = 50f; //threshold in m between playing nearSound and farSound
12     
public float minVolume = .4f;
13     
public float maxVolume = 1f;
14     
public float rolloffFactor = 0.5f;
15     
16     
private AudioSource _soundComponent;
17     
private bool _delayedExplosionStarted = false;
18     
private float _explodeDelay;
19     
20     
override public void Init()
21     {
22         _soundComponent = (AudioSource)gameObject.AddComponent <AudioSource>();
23     }
24
25     
void Update()
26     {
27         
if (_soundComponent == null) return;
28
29         _soundComponent.pitch = Time.timeScale;
30         
31         
if (_delayedExplosionStarted)
32         {
33             _explodeDelay = (_explodeDelay - Time.deltaTime);
34             
if (_explodeDelay <= 0f)
35             {
36                 Explode();
37             }
38         }
39     }
40     
41     
private int _idx;
42     
override public void Explode()
43     {
44         
if (detailThreshold > detail) return;
45     
46         
if (!_delayedExplosionStarted)
47         {
48             _explodeDelay = explodeDelayMin + (Random.
value * (explodeDelayMax - explodeDelayMin));
49         }
50         
if (_explodeDelay <= 0)
51         {
52     
// _soundComponent.minVolume = minVolume;
53     
// _soundComponent.maxVolume = maxVolume;
54     
// _soundComponent.rolloffFactor = rolloffFactor;
55             
56             
if (Vector3.Distance(Camera.main.transform.position, this.transform.position) < distanceThreshold)
57             {
58                 _idx = (
int)(Random.value * nearSounds.Length);
59                 _soundComponent.PlayOneShot(nearSounds[_idx]);
60             }
61             
else
62             {
63                 _idx = (
int)(Random.value * farSounds.Length);
64                 _soundComponent.PlayOneShot(farSounds[_idx]);
65             }
66             _delayedExplosionStarted =
false;
67             _explodeDelay =
0f;
68         }
69         
else
70         {
71             _delayedExplosionStarted =
true;
72         }
73     }
74     
75     
public void Reset()
76     {
77     }
78 }


Gõ tìm kiếm nhanh...